/******************************************************************************

	COPYRIGHT(C) JONAS 'SORTIE' TERMANSEN 2010.

	This file is part of Maxsi Client.

	Maxsi Client is free software: you can redistribute it and/or modify it
	under the terms of the GNU Lesser General Public License as published by
	the Free Software Foundation, either version 3 of the License, or (at your
	option) any later version.

	Maxsi Client is distributed in the hope that it will be useful, but WITHOUT
	ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
	FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
	License for more details.

	You should have received a copy of the GNU Lesser General Public License
	along with Maxsi Client. If not, see <http://www.gnu.org/licenses/>.

	Maxsi Client
	A client UI to the digital distribution platform Maxsi Distribution.

	PageClientMain.cpp
	The User Interface for the main window in Maxsi Client.

******************************************************************************/

#include "MaxsiLibrary.h"
#include "MaxsiDistribution.h"
#include "Main.h"
#include "UI.h"
#include "PageClientMain.h"
#include "PageInstallerInitializing.h"
#include "PageReleaseInfo.h"

MaxsiError CreateInstallerWindow()
{
	MaxsiError		Result			=	MAXSI_ERROR_SUCCESS;
	MaxsiWindow*	InstallWindow	=	NULL;
	Page*			InstallPage		=	NULL;

	if ( Result == MAXSI_ERROR_SUCCESS )
	{		
		InstallWindow	=	WindowSystem->CreateWindow(NULL, _MESTR("Window"), 500, 370, MAXSI_WINDOW_CENTER_X, MAXSI_WINDOW_CENTER_Y, _MESTR("Install a product"));

		if ( InstallWindow == NULL ) { Result = MAXSI_ERROR_MEM_ALLOC_FAIL; }
	}

	if ( Result == MAXSI_ERROR_SUCCESS )
	{
		InstallPage	=	new PageInstallerInitializing;

		if ( InstallPage == NULL )
		{
			Result = MAXSI_ERROR_MEM_ALLOC_FAIL;
		}
		else
		{
			InstallWindow->SetEventHandler(OnEvent, (BYTE*)InstallPage);

			InstallPage->SetWindow(InstallWindow);
		}
	}

	if ( Result == MAXSI_ERROR_SUCCESS ) { Result = InstallPage->CreateControls(); }

	if ( Result == MAXSI_ERROR_SUCCESS )
	{
		InstallWindow->Show();
	}

	if ( Result != MAXSI_ERROR_SUCCESS && InstallWindow != NULL ) { WindowSystem->DeleteWindow(InstallWindow); }
	if ( Result != MAXSI_ERROR_SUCCESS && InstallPage != NULL ) { delete InstallPage; }

	return Result;
}

MaxsiError CreateReleaseWindow()
{
	MaxsiError		Result			=	MAXSI_ERROR_SUCCESS;
	MaxsiWindow*	ReleaseWindow	=	NULL;
	Page*			ReleasePage		=	NULL;

	if ( Result == MAXSI_ERROR_SUCCESS )
	{		
		ReleaseWindow	=	WindowSystem->CreateWindow(NULL, _MESTR("Window"), 500, 370, MAXSI_WINDOW_CENTER_X, MAXSI_WINDOW_CENTER_Y, _MESTR("Publish a product"));

		if ( ReleaseWindow == NULL ) { Result = MAXSI_ERROR_MEM_ALLOC_FAIL; }
	}

	if ( Result == MAXSI_ERROR_SUCCESS )
	{
		ReleasePage	=	new PageReleaseInfo;

		if ( ReleasePage == NULL )
		{
			Result = MAXSI_ERROR_MEM_ALLOC_FAIL;
		}
		else
		{
			ReleaseWindow->SetEventHandler(OnEvent, (BYTE*)ReleasePage);

			ReleasePage->SetWindow(ReleaseWindow);
		}
	}

	if ( Result == MAXSI_ERROR_SUCCESS ) { Result = ReleasePage->CreateControls(); }

	if ( Result == MAXSI_ERROR_SUCCESS )
	{
		ReleaseWindow->Show();
	}

	if ( Result != MAXSI_ERROR_SUCCESS && ReleaseWindow != NULL ) { WindowSystem->DeleteWindow(ReleaseWindow); }
	if ( Result != MAXSI_ERROR_SUCCESS && ReleasePage != NULL ) { delete ReleasePage; }

	return Result;
}

PageClientMain::PageClientMain()
{
	InstallProduct		=	NULL;
	ReleaseProduct		=	NULL;
}

PageClientMain::~PageClientMain()
{
	
}

MaxsiError PageClientMain::CreateControls()
{
	InstallProduct		=	Window->CreateChild(_MESTR("Button"), 125, 32, Window->Width()-1*150, Window->Height()-60, _MESTR("Install a Product"));
	ReleaseProduct		=	Window->CreateChild(_MESTR("Button"), 125, 32, InstallProduct->Left()-1*150, Window->Height()-60, _MESTR("Publish a Product"));

	return MAXSI_ERROR_SUCCESS;
}

MaxsiError PageClientMain::DeleteControls()
{
	WindowSystem->DeleteWindow(InstallProduct);
	WindowSystem->DeleteWindow(ReleaseProduct);

	return MAXSI_ERROR_SUCCESS;
}

MaxsiError PageClientMain::OnEvent(MaxsiWindowEvent* Event)
{
	if ( Event->Owner == InstallProduct && Event->Type == MAXSI_EVENT_PRESSED )
	{
		return CreateInstallerWindow();
	}
	if ( Event->Owner == ReleaseProduct && Event->Type == MAXSI_EVENT_PRESSED )
	{
		return CreateReleaseWindow();
	}

	return MAXSI_ERROR_NOT_SUPPORTED;
}

